package commandimpl;

import java.util.Date;
import command.ContreteCommand;
import dao.Dao;
import main.Console;
import role.Memo;
import role.Tag;

public class SetupCommand extends ContreteCommand {
	public void action() {
		while (true) {
			System.out.println("请输入你所要创建的内容，输入0退出创建");
			System.out.println("1.创建备忘录");
			System.out.println("2.创建自定义标签");
			String choice = Console.askInput();
			switch (choice) {
			case "0":
				return;
			case "1":
				setupMemo();
				break;
			case "2":
				setupTag();
				break;
			default:
				continue;
			}

		}

	}

	public static void setupMemo() {

		while (true) {
			String title = Console.askInput("请输入要创建的备忘录的标题（45字以内）");
			String name = Console.askInput("请输入标签名");
			System.out.println("--                     --");
			String content = Console.askInput("请输入要创建的备忘录的内容（140字以内）");
			System.out.println("--                     --");
			System.out.println("--                     --");
			System.out.println("--                     --");
			String degree = Console.askInput("请输入此备忘录的重要程度");
			Date date = new Date(System.currentTimeMillis());
			String str = String.valueOf(date);
			try {
				Dao dao = new Dao();
				int tagId = dao.selectIdOfTag(name);
				Memo memo = new Memo(title, content, tagId, str, degree);
				dao.insertMemo(memo);
				dao.sessionClose();
			} catch (Exception e) {
				Console.println("查无此自定义标签，请先创建自定义标签！");
				return;
			}

			while (true) {
				String choice = Console.askInput("还要继续创建备忘录吗？Y/N");
				if ("N".equals(choice.toUpperCase())) {
					return;
				} else if ("Y".equals(choice.toUpperCase())) {
					break;
				}
			}

		}
	}

	private static void setupTag() {
		Dao dao = new Dao();
		while (true) {

			try {
				String name = Console.askInput("请输入自定义标签的名字");
				Tag tag = new Tag(name);

				dao.insertTag(tag);
			} catch (Exception e) {
				System.out.println("此标签已存在！请重新输入");
				continue;
			}
			dao.sessionClose();
			while (true) {
				String choice = Console.askInput("还要继续创建自定义书签吗？Y/N");
				if ("N".equals(choice.toUpperCase())) {
					return;
				} else if ("Y".equals(choice.toUpperCase())) {
					break;
				}
			}
		}
	}

}
